package com.stgl.action;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.stgl.service.HomeService;
import com.stgl.service.StudentService;
import com.stgl.service.WechartService;
import com.stgl.util.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/wechart")
public class wechart {
    @Resource
    private WechartService wechartService;
    @Resource
    private HomeService homeService;

    @Resource
    private StudentService studentService;
    /**
     * @author 牟明艳
     * @comments 成功获取到openid并进入连接开始业务处理
     * @time 2015-11-17 下午3:07:15
     */
    @RequestMapping("getwxuserinfo.html")
    public @ResponseBody Map<String, Object>getwxuserinfo(HttpServletRequest req){
        String tappid="wxbb59e4b8ebd0cf8d";
        String tappsecret="527545f9cb730d008007a0650c0af0ec";

        String code = req.getParameter("code");
        //第二步:通过code获取access_token
     /*   ConfKit confkit= new ConfKit("wxpay.properties");*/
//		String urlwx = "https://api.weixin.qq.com/sns/jscode2session?appid="+confkit.get("appid")+"&secret="+confkit.get("appsecret")+"&js_code="+code+"&grant_type=authorization_code";
        String urlwx = "https://api.weixin.qq.com/sns/jscode2session?appid="+tappid+"&secret="+tappsecret+"&js_code="+code+"&grant_type=authorization_code";
        System.out.println("urlwx:"+urlwx);
        String access_token_json = HttpClientUtil.sendGet(urlwx);
        Map<String,String> p_access_token = JSON.parseObject(access_token_json,HashMap.class);
        System.out.println("p_access_token:"+p_access_token);
        String openid = p_access_token.get("openid").toString();
        String session_key = p_access_token.get("session_key").toString();
        Map<String, Object>setmap=new HashMap<String, Object>();
        setmap.put("openid", openid);
        setmap.put("session_key", session_key);
        return setmap;
    }

    @RequestMapping("getSystemInfo.html")
    public @ResponseBody Map<String, Object>getSystemInfo(HttpServletRequest req){
        //获取系统信息
        Map<String, Object>smaps=new HashMap<String, Object>();
        smaps=homeService.getSystemInfo();
        Map<String, Object>setmap=new HashMap<String, Object>();
        setmap.put("bmjzrq", smaps.get("BMJZRQ"));
        setmap.put("rxstart", smaps.get("RXSTART"));
        setmap.put("rxend", smaps.get("RXEND"));
        setmap.put("sfkqbm", smaps.get("SFKQBM"));
        setmap.put("wxts", smaps.get("WXTS"));
        return setmap;
    }




    /**
     * @author 牟明艳
     * @comments 购买 提交订单
     * @time 2017年12月15日10:39:37
     */
    @RequestMapping("submitorder.html")
    public @ResponseBody Map<String, Object> submitorder(HttpServletRequest req){
        String orderid = req.getParameter("oid");
        String wxid = req.getParameter("wxid");
        String ordername = req.getParameter("ordername");


        //手续费 总金额的百分几
        String allprice = req.getParameter("allprice");//总价格
        String fs3 = req.getParameter("fs3");//手续费占百分几
        float f1=Float.parseFloat(allprice)*Float.parseFloat(fs3);
        double f2=f1*0.01;
        double zje=Double.parseDouble(allprice)+f2;
        DecimalFormat zhje = new DecimalFormat(".00");
        String zjg=zhje.format(zje);


        String ip = req.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = req.getRemoteAddr();
        }

        //支付
        Map<String, Object>paymap=new HashMap<String, Object>();
        long timeStamp_=System.currentTimeMillis();   //时间戳
        String payurl="https://api.mch.weixin.qq.com/pay/unifiedorder";
        String timeStamp=String.valueOf(timeStamp_);

        //金额转换
        float floatmonye=Float.parseFloat(zjg)*100;
        int Moneys=(int)floatmonye;
        SortedMap<String, Object>  treeMap = new TreeMap<String, Object>();

        String nonce_str=PayCommonUtil.CreateNoncestr();
        ConfKit confkit= new ConfKit("wxpay.properties");
        treeMap.put("appid",confkit.get("appid"));// 微信分配的小程序ID
        treeMap.put("body", "柒彩教育-"+ordername);  //商品简单描述  商家名称-销售商品类目
        treeMap.put("detail", "柒彩教育-"+ordername);  //单品优惠字段v
        treeMap.put("mch_id", confkit.get("mch_id")); //微信支付分配的商户号
        treeMap.put("nonce_str",nonce_str); //随机字符串，不长于32位。
        treeMap.put("notify_url", confkit.get("notify_url")+"/wechart/notify.html");  //接收微信支付异步通知回调地址
        treeMap.put("openid",wxid); //用户在商户appid下的唯一标识。openid如何获取
        treeMap.put("out_trade_no",orderid);  //商户系统内部的订单号
        treeMap.put("spbill_create_ip", ip);
        treeMap.put("total_fee", Moneys+"");  //订单总金额，单位为分
        treeMap.put("trade_type", "JSAPI"); //交易类型
        String sign=PayCommonUtil.createSign(treeMap,confkit);//生成签名的方法
        treeMap.put("sign", sign);  //签名

        String xmltreemap="<xml>"+
                "<appid>"+confkit.get("appid")+"</appid>"+
                "<body>柒彩教育-"+ordername+"</body>"+
                "<detail>柒彩教育-"+ordername+"</detail>"+
                "<mch_id>"+confkit.get("mch_id")+"</mch_id>"+
                "<nonce_str>"+nonce_str+"</nonce_str>"+
                "<notify_url>"+confkit.get("notify_url")+"/wechart/notify.html"+"</notify_url>"+
                "<openid>"+wxid+"</openid>"+
                "<out_trade_no>"+orderid+"</out_trade_no>"+
                "<spbill_create_ip>"+ip+"</spbill_create_ip>"+
                "<total_fee>"+Moneys+""+"</total_fee>"+
                "<trade_type>JSAPI</trade_type>"+
                "<sign>"+sign+"</sign>"+
                "</xml>";
        String prepay_id= GetWxOrderno.getPayNo(payurl, xmltreemap);

        //再次签名
        String sb="appId="+confkit.get("appid")+"&";
        sb+="nonceStr="+nonce_str+"&";
        sb+="package=prepay_id="+prepay_id+"&";
        sb+="signType=MD5&timeStamp="+timeStamp+"&";
        sb+="key="+confkit.get("key");

        String paysign = MD5Util.MD5Encode(sb.toString(),"UTF-8").toUpperCase();
        paymap.put("timeStamp", timeStamp);
        paymap.put("nonceStr", nonce_str);
        paymap.put("prepay_id", prepay_id);
        paymap.put("sign", paysign);
        paymap.put("orderid", orderid);

        System.out.println("paymap:"+paymap);
        return paymap;
    }

    /**
     * @author 牟明艳
     * @comments 添加购物车
     * @time 2017年10月25日18:50:05
     */
    @RequestMapping("notify.html")
    public @ResponseBody void notify(HttpServletRequest req){
        System.out.print("微信支付回调数据开始");
        //示例报文
        String inputLine;
        String notityXml = "";
        String resXml = "";
        try {
            while ((inputLine = req.getReader().readLine()) != null) {
                notityXml += inputLine;
            }
            req.getReader().close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("接收到的报文：" + notityXml);

        System.out.println("微信支付回调数据结束");
        return ;
    }



    /**
     * @author 牟明艳
     * @comments 根据openid查询用户绑定的学生人员有
     * @time 2019年5月27日16:45:35
     */
    @RequestMapping("getuserBindStu.html")
    public @ResponseBody List<Map<String,Object>>getuserBindStu(HttpServletRequest req){
        String wxid = req.getParameter("wxid");
        Map<String, Object>setmap=new HashMap<String, Object>();
        setmap.put("wxid", wxid);
        List<Map<String,Object>>list1=wechartService.getuserBindStu(setmap);
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 查询所有校区
     * @time 2019年7月2日17:33:40
     */
    @RequestMapping("getAllSchool.html")
    public @ResponseBody List<Map<String,Object>>getAllSchool(HttpServletRequest req){
        List<Map<String,Object>>list1=wechartService.getAllSchool();
        return list1;
    }


    /**
     * @author 牟明艳
     * @comments 查询播州区的地区
     * @time 2019年7月4日10:25:34
     */
    @RequestMapping("getBzqArea.html")
    public @ResponseBody List<Map<String,Object>>getBzqArea(HttpServletRequest req){
        List<Map<String,Object>>list1=wechartService.getBzqArea();
        return list1;
    }




    /*
     * 上传图片
     */
    @RequestMapping("uploadBmurl.html")
    public  @ResponseBody Map<String, Object> uploadBmurl(HttpServletRequest request, HttpServletResponse response){
        Map<String, Object>param=new HashMap<String, Object>();
        MultipartHttpServletRequest mhsr = (MultipartHttpServletRequest)request;
        if (mhsr == null) {
            return param;
        }
        DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        upload.setHeaderEncoding("utf-8");
        try
        {
            mhsr.setCharacterEncoding("utf-8");
        }
        catch (UnsupportedEncodingException e1)
        {
            e1.printStackTrace();
        }
        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);

        String se = File.separator;
        String savePath = mhsr.getRealPath("upload")+se +nowyear+se+ "bmqk";
        File f1 = new File(savePath);
        if (!f1.exists()) {
            f1.mkdirs();
        }
        String strfile = "";
        try
        {
            Map<String, MultipartFile> files = mhsr.getFileMap();
            //先进行检测
            for (String key : files.keySet())
            {
                MultipartFile mf = mhsr.getFile(key);
                Map<String,Object> rmap=SecCheck.imgSecCheck(mf);
                if (rmap.get("result").toString().equals("false")) {
                    return rmap;
                }
            }
            //然后再进行插入
            for (String key : files.keySet())
            {
                MultipartFile mf = mhsr.getFile(key);

                String name = mf.getOriginalFilename();
                String extName = "";
                if ((name != null) && (!name.trim().equals("")))
                {
                    if (name.lastIndexOf(".") >= 0) {
                        extName = name.substring(name.lastIndexOf("."));
                    }
                    Long fname = Long.valueOf(System.nanoTime());
                    File file = new File(savePath + se + fname + extName);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    mf.transferTo(file);
                    strfile = "upload" + "\\//" + nowyear+"\\//bmqk" + "\\//"  + fname + extName;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        param.put("urls", strfile);
        param.put("result",true);
        return param;
    }

    /**
     * @author 牟明艳
     * @comments  学生报名(2021的版本)
     * @time 2021年2月9日12:26:35
     */
    @RequestMapping("insertStudent1.html")
    public @ResponseBody Map<String,Object> insertStudent1(HttpServletRequest req)throws Exception {
        Map<String, Object>rmap=new HashMap<String, Object>();
        String csid = req.getParameter("csid");//校区id
        String csnum= req.getParameter("csnum");//校区序号
        String sname = req.getParameter("sname");//儿童姓名
        String jzxm = req.getParameter("jzxm");//家长姓名
        String jzsj = req.getParameter("jzsj");//家长手机号

        String wxid = req.getParameter("wxid");//微信id
        String formid = req.getParameter("formid");//formid

        //查询系统是否已经关闭
        //查询系统信息
        Map<String,Object>sysinfo=wechartService.getSystemInfo();
        if(sysinfo.get("SFKQBM")!=null){
            String sfkqbm=sysinfo.get("SFKQBM").toString();
            if(sfkqbm.equals("1")){
                rmap.put("result",false);
                rmap.put("msg","新生登记结束，等待摇号！");
                return rmap;
            }
        }

        //用户必须上传图片
        String urls = req.getParameter("urls");//图片路径
        List<String> listr = (List<String>) JSONArray.parse(urls);
        if(listr.size()<=0){
            rmap.put("result",false);
            rmap.put("msg","请上传户口本户主页以及登记儿童所在户口页！");
            return rmap;
        }

        String birth= req.getParameter("birth");//获取儿童生日
        //对生日进行判断与转换为正规格式20211009
        rmap=IDCardUtil.CheckBirth(birth);
        if(rmap.get("result").toString().equals("false")){
            return rmap;
        }else {
            birth=rmap.get("birth").toString();
        }
        //格式用2020-10-09
        String csrq=IDCardUtil.changBirthStr2(birth);
        String sex= req.getParameter("sex");//获取儿童性别

        if(sysinfo.get("RXSTART")!=null && sysinfo.get("RXEND")!=null){
            String rxstart=sysinfo.get("RXSTART").toString().replaceAll("-","");
            if(Integer.parseInt(birth) < Integer.parseInt(rxstart) ){
                rmap.put("result",false);
                rmap.put("msg","对不起,您的孩子不符合报名条件,儿童出生年月应在"+sysinfo.get("RXSTART").toString()+"与"+sysinfo.get("RXEND").toString()+"之间");
                return rmap;
            }

            String rxend=sysinfo.get("RXEND").toString().replaceAll("-","");
            if(Integer.parseInt(birth) > Integer.parseInt(rxend) ){
                rmap.put("result",false);
                rmap.put("msg","对不起,您的孩子不符合报名条件,儿童出生年月应在"+sysinfo.get("RXSTART").toString()+"与"+sysinfo.get("RXEND").toString()+"之间");
                return rmap;
            }
        }else{
            String jzrq="";
            if(sysinfo.get("BMJZRQ")!=null){
                jzrq=sysinfo.get("BMJZRQ").toString().substring(0,10);
            }else{
                SimpleDateFormat df0 = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                jzrq=df0.format(new Date());
            }
            Map<String,Object>sfmap=new HashMap<>();
            sfmap.put("birth",csrq);
            sfmap.put("jzrq",jzrq);
            String age=wechartService.getAgeByBirth(sfmap);
            if(Integer.parseInt(age)<2 || Integer.parseInt(age)>3){
                rmap.put("result",false);
                rmap.put("msg","对不起,您的孩子不符合报名条件,儿童年龄应在2-3岁");
                return rmap;
            }
        }


        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);
        //格式用2020年10月09日
        String nyr=IDCardUtil.changBirthStr(birth);
        //学校报名  今年没录取上  明年还可以报
        //查询该学生今年是否报名了其它学校
        Map<String,Object>nmap=new HashMap<>();
        nmap.put("birth",nyr);
        nmap.put("sname",sname);
        nmap.put("nowyear",nowyear);
        nmap.put("sex",sex);
        List<Map<String,Object>>listcam=wechartService.getCampusStudentByBirthAndName(nmap);
        if(listcam.size()>0){
            rmap.put("result",false);
            rmap.put("msg","对不起,你已经报名了其它学校,不能重复报名!");
            return rmap;
        }

        //根据儿童出生年月获取所在年级
        int nj=wechartService.getNjByCsrq(csrq);

        Map<String, Object>gmap=new HashMap<String, Object>();
        String sid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("sid",sid);
        gmap.put("sname",sname);
        String tsrc = req.getParameter("tsrc");
        if(tsrc.equals("1")){
            String hjszd = req.getParameter("hjszd");//户籍所在地
            gmap.put("hjszd",hjszd);
        }else{
            String town = req.getParameter("town");//选择的镇
            String cun = req.getParameter("cun");//选择的村
            gmap.put("hjszd",town+","+cun);
        }
        gmap.put("tsrc",tsrc);
        gmap.put("jzxm",jzxm);
        gmap.put("jzsj",jzsj);
        gmap.put("csid",csid);
        // gmap.put("bmh",bmh);
        gmap.put("sfbmcg","0");
        // gmap.put("sfzh",sfzh);
        gmap.put("birth",nyr);
        gmap.put("sex",sex);
        gmap.put("wxid",wxid);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        gmap.put("bmsj",df.format(new Date()));
        gmap.put("sfqx","0");
        gmap.put("rxnj",nj+"");
        gmap.put("formid",formid);
        //以下信息用于计算报名号
        gmap.put("nowyear",String.valueOf(nowyear));
        gmap.put("csnum",csnum);
        gmap.put("nj",nj+"");

        //文字检测
        //检测内容是否违规
        String pjson= JSON.toJSONString(gmap);
        rmap= SecCheck.msgSecCheck(pjson);
        if(rmap.get("result").toString().equals("false")){
            return rmap;
        }

        //报名
        rmap=wechartService.BmxtInsert(listr,gmap);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments  学生报名(2019的版本)
     * @time 2019年7月4日14:59:52
     */
    @RequestMapping("insertStudent.html")
    public @ResponseBody Map<String,Object> insertStudent(HttpServletRequest req)throws Exception{
        Map<String, Object>rmap=new HashMap<String, Object>();
        String csid = req.getParameter("csid");//校区id
        String csnum= req.getParameter("csnum");//校区序号
        String sname = req.getParameter("sname");//儿童姓名
        String sfzh = req.getParameter("sfzh");//儿童身份证号
        String jzxm = req.getParameter("jzxm");//家长姓名
        String jzsj = req.getParameter("jzsj");//家长手机号

        String wxid = req.getParameter("wxid");//微信id
        String formid = req.getParameter("formid");//formid

        //查询系统是否已经关闭
        //查询系统信息
        Map<String,Object>sysinfo=wechartService.getSystemInfo();
        if(sysinfo.get("SFKQBM")!=null){
            String sfkqbm=sysinfo.get("SFKQBM").toString();
            if(sfkqbm.equals("1")){
                rmap.put("result",false);
                rmap.put("msg","新生登记结束，等待摇号！");
                return rmap;
            }
        }

        //用户必须上传图片
        String urls = req.getParameter("urls");//图片路径
        List<String> listr = (List<String>) JSONArray.parse(urls);
        if(listr.size()<=0){
            rmap.put("result",false);
            rmap.put("msg","请上传户口本户主页以及登记儿童所在户口页！");
            return rmap;
        }

        //验证身份证是否正确
        String idvaild=IDCardUtil.IDCardValidate(sfzh);
        if(!idvaild.equals("1")){
            rmap.put("result",false);
            rmap.put("msg",idvaild);
            return rmap;
        }
        String birth=IDCardUtil.getbirth(sfzh);
        String sex=IDCardUtil.getsex(sfzh);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

        if(sysinfo.get("RXSTART")!=null && sysinfo.get("RXEND")!=null){
            String birth1=IDCardUtil.getbirth1(sfzh);
            String rxstart=sysinfo.get("RXSTART").toString().replaceAll("-","");
            if(Integer.parseInt(birth1) < Integer.parseInt(rxstart) ){
                rmap.put("result",false);
                rmap.put("msg","对不起,您的孩子不符合报名条件,儿童出生年月应在"+sysinfo.get("RXSTART").toString()+"与"+sysinfo.get("RXEND").toString()+"之间");
                return rmap;
            }

            String rxend=sysinfo.get("RXEND").toString().replaceAll("-","");
            if(Integer.parseInt(birth1) > Integer.parseInt(rxend) ){
                rmap.put("result",false);
                rmap.put("msg","对不起,您的孩子不符合报名条件,儿童出生年月应在"+sysinfo.get("RXSTART").toString()+"与"+sysinfo.get("RXEND").toString()+"之间");
                return rmap;
            }
        }else{
            String jzrq="";
            if(sysinfo.get("BMJZRQ")!=null){
                jzrq=sysinfo.get("BMJZRQ").toString().substring(0,10);
            }else{
                SimpleDateFormat df0 = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                jzrq=df0.format(new Date());
            }
            String birth2=IDCardUtil.getbirth2(sfzh);
            Map<String,Object>sfmap=new HashMap<>();
            sfmap.put("birth",birth2);
            sfmap.put("jzrq",jzrq);
            String age=wechartService.getAgeByBirth(sfmap);
            if(Integer.parseInt(age)<2 || Integer.parseInt(age)>3){
                rmap.put("result",false);
                rmap.put("msg","对不起,您的孩子不符合报名条件,儿童年龄应在2-3岁");
                return rmap;
            }
        }


        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);

        //学校报名  今年没录取上  明年还可以报
        //查询该学生今年是否报名了其它学校
        Map<String,Object>nmap=new HashMap<>();
        nmap.put("sfzh",sfzh);
        nmap.put("nowyear",nowyear);
        List<Map<String,Object>>listcam=wechartService.getCampusStudentBysfzh(nmap);
        if(listcam.size()>0){
            rmap.put("result",false);
            rmap.put("msg","对不起,你已经报名了其它学校,不能重复报名!");
            return rmap;
        }

        //根据儿童出生年月获取所在年级
        String csrq=IDCardUtil.getbirth2(sfzh);
        int nj=wechartService.getNjByCsrq(csrq);

        //查询该学校的学生人数
        /*Map<String,Object>bmap=new HashMap<>();
        bmap.put("csid",csid);
        bmap.put("nowyear",String.valueOf(nowyear));
        bmap.put("csnum",csnum);
        bmap.put("nj",nj+"");
        String bmh=wechartService.getCampusStudentNextBmh(bmap);*/


        Map<String, Object>gmap=new HashMap<String, Object>();
        String sid = UUID.randomUUID().toString().replaceAll("-", "");
        gmap.put("sid",sid);
        gmap.put("sname",sname);
        String tsrc = req.getParameter("tsrc");
        if(tsrc.equals("1")){
            String hjszd = req.getParameter("hjszd");//户籍所在地
            gmap.put("hjszd",hjszd);
        }else{
            String town = req.getParameter("town");//选择的镇
            String cun = req.getParameter("cun");//选择的村
            gmap.put("hjszd",town+","+cun);
        }
        gmap.put("tsrc",tsrc);
        gmap.put("jzxm",jzxm);
        gmap.put("jzsj",jzsj);
        gmap.put("csid",csid);
        // gmap.put("bmh",bmh);
        gmap.put("sfbmcg","0");
        gmap.put("sfzh",sfzh);
        gmap.put("birth",birth);
        gmap.put("sex",sex);
        gmap.put("wxid",wxid);
        gmap.put("bmsj",df.format(new Date()));
        gmap.put("sfqx","0");
        gmap.put("rxnj",nj+"");
        gmap.put("formid",formid);
        //以下信息用于计算报名号
        gmap.put("nowyear",String.valueOf(nowyear));
        gmap.put("csnum",csnum);
        gmap.put("nj",nj+"");
        //文字检测
        //检测内容是否违规
        String pjson= JSON.toJSONString(gmap);
        rmap= SecCheck.msgSecCheck(pjson);
        if(rmap.get("result").toString().equals("false")){
            return rmap;
        }


        rmap=wechartService.BmxtInsert(listr,gmap);
        //先上传图片
        /*for(int k=0;k<listr.size();k++){
            Map<String, Object>zmap=new HashMap<String, Object>();
            zmap.put("imageurl",listr.get(k).toString());
            zmap.put("tablename","student");
            zmap.put("zjid",sid);
            zmap.put("zdmc","hjszd");
            int m=studentService.Insert_stufile(zmap);
            if(m<0){
                rmap.put("result",false);
                rmap.put("msg","图片上传失败!");
                return rmap;
            }
        }*/

        /*int i=wechartService.insertStudent(gmap);
        if(i>0){
            rmap.put("result",true);
            rmap.put("msg","报名成功！");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","报名失败,请联系学校管理员！");*/
        return rmap;
    }



    /**
     * @author 牟明艳
     * @comments 查询该微信号报名的所有学校
     * @time 2019年7月4日10:25:34
     */
    @RequestMapping("studentList.html")
    public @ResponseBody Map<String,Object>studentList(HttpServletRequest req){
        String wxid = req.getParameter("wxid");//微信id
        String page = req.getParameter("page");//页数
        if(page==null || page.trim().equals("")){
            page="1";
        }
        String limit = req.getParameter("limit");//每页条数
        if(limit==null || limit.trim().equals("")){
            limit="10";
        }
        Integer end=Integer.parseInt(page)*Integer.parseInt(limit);
        Integer start=end-10;
        Map<String, Object>gmap=new HashMap<String, Object>();
        gmap.put("wxid", wxid);
        gmap.put("start", start);
        gmap.put("end",end);

        Map<String, Object>rmap=new HashMap<String, Object>();
        List<Map<String,Object>>list1=wechartService.studentList(gmap);
        rmap.put("items", list1);

        //查询学校总数
        Integer total=0;
        total=wechartService.studentCount(gmap);
        gmap.put("total", total);
        rmap.put("paginate", gmap);
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments  取消报名
     * @time 2019年7月5日10:02:31
     */
    @RequestMapping("cancelBM.html")
    public @ResponseBody Boolean cancelBM(HttpServletRequest req){
        Map<String,Object>sysinfo=wechartService.getSystemInfo();
        if(sysinfo.get("SFKQBM")!=null){
            String sfkqbm=sysinfo.get("SFKQBM").toString();
            if(sfkqbm.equals("1")){
                return false;
            }
        }

        String sid = req.getParameter("sid");
        int i=wechartService.cancelBM(sid);
        if(i>0){
            return true;
        }
        return false;
    }



    /**
     * @author 牟明艳
     * @comments 查询学校详细信息
     * @time 2019年7月5日10:46:51
     */
    @RequestMapping("studentDetail.html")
    public @ResponseBody Map<String,Object>studentDetail(HttpServletRequest req){
        String sid = req.getParameter("sid");
        Map<String,Object>mapss=wechartService.studentDetail(sid);
        return mapss;
    }



    /**
     * @author 牟明艳
     * @comments 查询学校详细信息
     * @time 2019年7月5日10:46:51
     */
    @RequestMapping("studentCamArea.html")
    public @ResponseBody Map<String,Object>studentCamArea(HttpServletRequest req){
        String sid = req.getParameter("sid");
        Map<String,Object>mapss=wechartService.studentDetail(sid);

        List<Map<String,Object>>cam=wechartService.getAllSchool();
        List<Map<String,Object>>area=wechartService.getBzqArea();

        mapss.put("cam",cam);
        mapss.put("area",area);
        return mapss;
    }





    /**
     * @author 牟明艳
     * @comments  学生报名
     * @time 2019年7月4日14:59:52
     */
    @RequestMapping("updateStudent.html")
    public @ResponseBody Map<String,Object> updateStudent(HttpServletRequest req)throws Exception{
        Map<String, Object>rmap=new HashMap<String, Object>();
        String sid = req.getParameter("sid");//学生id
        String csid = req.getParameter("csid");//校区id
        String csido = req.getParameter("csido");//老校区id
        String csnum= req.getParameter("csnum");//校区序号
        String sname = req.getParameter("sname");//儿童姓名
        String sfzh = req.getParameter("sfzh");//儿童身份证号
        String jzxm = req.getParameter("jzxm");//家长姓名
        String jzsj = req.getParameter("jzsj");//家长手机号
        String formid = req.getParameter("formid");//formid

        //查询系统是否已经关闭
        //查询系统信息
        Map<String,Object>sysinfo=wechartService.getSystemInfo();
       if(sysinfo.get("SFKQBM")!=null){
            String sfkqbm=sysinfo.get("SFKQBM").toString();
            if(sfkqbm.equals("1")){
                rmap.put("result",false);
                rmap.put("msg","新生登记结束，等待摇号！");
                return rmap;
            }
        }

        String urls = req.getParameter("urls");//图片路径
        List<String> listr = (List<String>) JSONArray.parse(urls);
        if(listr.size()<=0){
            rmap.put("result",false);
            rmap.put("msg","请上传户口本户主页以及登记儿童所在户口页！");
            return rmap;
        }

        //验证身份证是否正确
        String idvaild=IDCardUtil.IDCardValidate(sfzh);
        if(!idvaild.equals("1")){
            rmap.put("result",false);
            rmap.put("msg",idvaild);
            return rmap;
        }

        String birth=IDCardUtil.getbirth(sfzh);
        String sex=IDCardUtil.getsex(sfzh);

        //查询系统信息
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //如果学校相同 则不用检测(在登记的时候已经检测了，且身份证不能修改) 如果学校不同，则需要检测
        if(!csido.equals(csid)){
            if(sysinfo.get("RXSTART")!=null && sysinfo.get("RXEND")!=null){
                String birth1=IDCardUtil.getbirth1(sfzh);
                String rxstart=sysinfo.get("RXSTART").toString().replaceAll("-","");
                if(Integer.parseInt(birth1) < Integer.parseInt(rxstart) ){
                    rmap.put("result",false);
                    rmap.put("msg","对不起,您的孩子不符合报名条件,儿童出生年月应在"+sysinfo.get("RXSTART").toString()+"之后");
                    return rmap;
                }

                String rxend=sysinfo.get("RXEND").toString().replaceAll("-","");
                if(Integer.parseInt(birth1) > Integer.parseInt(rxend) ){
                    rmap.put("result",false);
                    rmap.put("msg","对不起,您的孩子不符合报名条件,儿童出生年月应在"+sysinfo.get("RXEND").toString()+"之前");
                    return rmap;
                }
            }else{
                String jzrq="";
                if(sysinfo.get("BMJZRQ")!=null){
                    jzrq=sysinfo.get("BMJZRQ").toString().substring(0,10);
                }else{
                    SimpleDateFormat df0 = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                    jzrq=df0.format(new Date());
                }
                String birth2=IDCardUtil.getbirth2(sfzh);
                Map<String,Object>sfmap=new HashMap<>();
                sfmap.put("birth",birth2);
                sfmap.put("jzrq",jzrq);
                String age=wechartService.getAgeByBirth(sfmap);
                if(Integer.parseInt(age)<2 || Integer.parseInt(age)>3){
                    rmap.put("result",false);
                    rmap.put("msg","对不起,您的孩子不符合报名条件,儿童年龄应在2-3岁");
                    return rmap;
                }
            }
        }

        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);

        //学校报名  今年没录取上  明年还可以报
        //查询该学生今年是否报名了其它学校
        Map<String,Object>nmap=new HashMap<>();
        nmap.put("sfzh",sfzh);
        nmap.put("nowyear",String.valueOf(nowyear));

        //查询该学生是否报名了其它学校
        List<Map<String,Object>>listcam=wechartService.getCampusStudentBysfzh(nmap);
        if(listcam.size()>0){
            Map<String,Object>cmaps=new HashMap<>();
            cmaps=listcam.get(0);
            String sid1=cmaps.get("SID").toString();
            if(!sid.equals(sid1)){
                rmap.put("result",false);
                rmap.put("msg","对不起,你已经报名了其它学校,不能重复报名!");
                return rmap;
            }
        }


        Map<String, Object>gmap=new HashMap<String, Object>();
        //获取该学生的报名号
        if(!csido.equals(csid)){
            //根据儿童出生年月获取所在年级
            String csrq=IDCardUtil.getbirth2(sfzh);
            int nj=wechartService.getNjByCsrq(csrq);

            Map<String,Object>bmap=new HashMap<>();
            bmap.put("csid",csid);
            bmap.put("nowyear",String.valueOf(nowyear));
            bmap.put("csnum",csnum);
            bmap.put("nj",nj+"");
            String bmh=wechartService.getCampusStudentNextBmh(bmap);
            gmap.put("bmh",bmh);
        }
        gmap.put("sid",sid);
        gmap.put("sname",sname);

        String tsrc = req.getParameter("tsrc");//特殊人员
        //如果为特殊人员儿童 则户籍所在地为手填
        if(tsrc.equals("1")){
            String hjszd = req.getParameter("hjszd");//户籍所在地
            gmap.put("hjszd",hjszd);
        }else{
            String town = req.getParameter("town");//选择的镇
            String cun = req.getParameter("cun");//选择的村
            gmap.put("hjszd",town+","+cun);
        }
        gmap.put("jzxm",jzxm);
        gmap.put("jzsj",jzsj);
        gmap.put("csid",csid);
        gmap.put("sfzh",sfzh);
        gmap.put("birth",birth);
        gmap.put("sex",sex);
        gmap.put("ccsh","0");//修改之后 初审变为未审核
        gmap.put("sbyy","");//失败的原因为空
        gmap.put("formid",formid);//失败的原因为空


        Map<String, Object>simap=new HashMap<String, Object>();
        simap.put("tablename","student");
        simap.put("zjid",sid);
        simap.put("zdmc","hjszd");
        int j=studentService.del_stufile(simap);
        for(int k=0;k<listr.size();k++){
            Map<String, Object>zmap=new HashMap<String, Object>();
            zmap.put("imageurl",listr.get(k).toString());
            zmap.put("tablename","student");
            zmap.put("zjid",sid);
            zmap.put("zdmc","hjszd");
            int m=studentService.Insert_stufile(zmap);
            if(m<0){
                rmap.put("result",false);
                rmap.put("msg","图片上传失败!");
                return rmap;
            }
        }

        int i=wechartService.updateStudent(gmap);
        if(i>0){
            rmap.put("result",true);
            rmap.put("msg","修改成功！");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","修改失败,请联系学校管理员！");
        return rmap;
    }


    /**
     * @author 牟明艳
     * @comments  获取中签人员名单
     * @time 2019年7月30日14:13:55
     */
    @RequestMapping("getBmcgList.html")
    public @ResponseBody List<Map<String,Object>> getBmcgList(HttpServletRequest req){
        //获取校区
        List<Map<String,Object>>cam=wechartService.getAllSchool();
        for(int i=0;i<cam.size();i++){
            Map<String,Object>maps=cam.get(i);
            String csid=maps.get("CSID").toString();
            List<Map<String,Object>>list1=new ArrayList<>();
            list1=wechartService.getBmcgListByCsid(csid);
            maps.put("stu",list1);
        }
        return cam;
    }

    @RequestMapping("insertTsm.html")
    public @ResponseBody boolean insertTsm(HttpServletRequest req){
        String formId=req.getParameter("formId");
        Map<String,Object>maps=new HashMap<>();
        maps.put("formId",formId);
        return wechartService.insertTsm(maps);
    }


    @RequestMapping("getbmh.html")
    public @ResponseBody String getbmh(HttpServletRequest req){
        Map<String,Object>bmap=new HashMap<>();
        bmap.put("csid","1478912104212826");
        bmap.put("nowyear","2019");
        bmap.put("csnum","11");
        bmap.put("nj","2");
        return wechartService.getCampusStudentNextBmh(bmap);
    }
}
